package com.acwing.partition4;

import java.io.*;
import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author `RKC`
 * @date 2022/1/26 12:31
 */
@SuppressWarnings("all")
public class AC368银河 {

    private static final int N = 100010, M = 600010, INF = 0x3f3f3f3f;
    private static int[] h = new int[N], hs = new int[N], e = new int[M], ne = new int[M], w = new int[M];

    private static int n = 0, m = 0, idx = 0, cnt = 0, timestamp = 0;

    private static int[] dfn = new int[N], low = new int[N], scc = new int[N], size = new int[N], dist = new int[N];
    private static Deque<Integer> stk = new ArrayDeque<>(N);
    private static boolean[] inStk = new boolean[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        for (int i = 0; i < N; i++) h[i] = hs[i] = -1;
        for (int i = 1; i <= n; i++) add(h, 0, i, 1);
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split(" ");
            int t = Integer.parseInt(s[0]), a = Integer.parseInt(s[1]), b = Integer.parseInt(s[2]);
            if (t == 1) {
                add(h, b, a, 0);
                add(h, a, b, 0);
            } else if (t == 2) {
                add(h, a, b, 1);
            } else if (t == 3) {
                add(h, b, a, 0);
            } else if (t == 4) {
                add(h, b, a, 1);
            } else add(h, a, b, 0);
        }
        //从超级源点开始做tarjan算法就能访问到所有点
        tarjan(0);
        //缩点重建DAG图
        boolean flag = true;
        for (int u = 0; u <= n; u++) {
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i], a = scc[u], b = scc[v];
                if (a == b && w[i] > 0) {
                    flag = false;
                    break;
                } else add(hs, a, b, w[i]);
            }
            if (!flag) break;
        }
        if (!flag) writer.write("-1\n");
        else {
            //根据DAG图跑一遍dp的最长路
            for (int u = cnt; u > 0; u--) {
                for (int i = hs[u]; i != -1; i = ne[i]) {
                    int v = e[i];
                    dist[v] = Math.max(dist[v], dist[u] + w[i]);
                }
            }
            long ans = 0;
            for (int i = 1; i <= cnt; i++) ans += dist[i] * size[i];
            writer.write(ans + "\n");
        }
        writer.flush();
    }

    private static void tarjan(int u) {
        dfn[u] = low[u] = ++timestamp;
        stk.addLast(u);
        inStk[u] = true;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (dfn[v] == 0) {
                tarjan(v);
                low[u] = Math.min(low[u], low[v]);
            } else if (inStk[v]) {
                low[u] = Math.min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            int v = -1;
            cnt++;
            do {
                v = stk.pollLast();
                inStk[v] = false;
                scc[v] = cnt;
                size[cnt]++;
            } while (u != v);
        }
    }

    private static void add(int[] h, int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }
}
